﻿' Frowser - http://www.frowser.net
' Copyright (c) 2010
' by Will Morgenweck, Active Modules, Inc.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
Namespace Frowser.Controllers
    Public Class DNNForums
        Friend Function ForumsList(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal u As UserInfo) As List(Of ForumInfo)
            Dim groups As New DotNetNuke.Modules.Forum.GroupController
            Dim groupList As List(Of DotNetNuke.Modules.Forum.GroupInfo) = groups.GroupGetAllAuthorized(ModuleId, u.UserId, True)
            Dim listForums As New List(Of ForumInfo)
            For Each g As DotNetNuke.Modules.Forum.GroupInfo In groupList
                Dim fc As New DotNetNuke.Modules.Forum.ForumController
                Dim fl As List(Of DotNetNuke.Modules.Forum.ForumInfo) = fc.ForumGetAllByParentID(0, g.GroupID, True)
                For Each f As DotNetNuke.Modules.Forum.ForumInfo In fl
                    Dim ff As New ForumInfo
                    With ff
                        .ForumId = f.ForumID
                        .ForumDescription = f.Description
                        .ForumName = f.Name
                        .GroupId = g.GroupID
                        .GroupName = g.Name
                        .LastPostSubject = String.Empty
                        .LastPostUserId = f.MostRecentPostAuthorID
                        .TotalReplies = f.TotalPosts
                        .TotalTopics = f.TotalThreads
                        .ModuleId = f.ModuleID
                        .CanRead = String.Empty
                    End With
                    listForums.Add(ff)
                Next
            Next

            Return listForums
        End Function
        Friend Function GetTopics(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, ByVal u As UserInfo, ByVal RowIndex As Integer, ByVal PageSize As Integer) As List(Of ContentInfo)
            Dim fc As New DotNetNuke.Modules.Forum.ForumController
            Dim fi As DotNetNuke.Modules.Forum.ForumInfo = fc.GetForum(ForumId)
            If fi Is Nothing Then
                Return Nothing
            End If
            If Not fi.ForumPermissions Is Nothing AndAlso fi.ForumPermissions.Count > 0 Then
                If Not DotNetNuke.Modules.Forum.ForumPermissionController.HasForumPermission(fi.ForumPermissions, "VIEW") Then
                    Return Nothing
                End If
            End If


            Dim tc As New DotNetNuke.Modules.Forum.ThreadController
            Dim tl As List(Of DotNetNuke.Modules.Forum.ThreadInfo) = tc.ThreadGetAll(ModuleId, ForumId, PageSize, RowIndex, String.Empty, PortalId)
            Dim topicsList As List(Of ContentInfo) = Nothing
            If Not tl Is Nothing AndAlso tl.Count > 0 Then
                topicsList = New List(Of ContentInfo)
            End If
            For Each t As DotNetNuke.Modules.Forum.ThreadInfo In tl
                Dim ci As New ContentInfo
                With ci
                    .AuthorId = t.StartedByUserID
                    .AuthorName = t.StartedByUser.DisplayName
                    .Body = t.Body
                    .DateCreated = CStr(t.CreatedDate)
                    .ForumId = t.ForumID
                    .NextItem = t.NextThreadID
                    .PrevItem = t.PreviousThreadID
                    .ReplyCount = t.Replies
                    .Subject = t.Subject
                    .TopicId = t.ThreadID
                End With
                topicsList.Add(ci)
            Next
            Return topicsList
        End Function
        Friend Function GetTopic(ByVal PortalId As Integer, ByVal ModuleId As Integer, ByVal ForumId As Integer, ByVal TopicId As Integer, ByVal u As UserInfo, ByVal RowIndex As Integer, ByVal Rows As Integer) As List(Of ContentInfo)
            Dim fc As New DotNetNuke.Modules.Forum.ForumController
            Dim fi As DotNetNuke.Modules.Forum.ForumInfo = fc.GetForum(ForumId)
            If fi Is Nothing Then
                Return Nothing
            End If
            If Not fi.ForumPermissions Is Nothing AndAlso fi.ForumPermissions.Count > 0 Then
                If Not DotNetNuke.Modules.Forum.ForumPermissionController.HasForumPermission(fi.ForumPermissions, "VIEW") Then
                    Return Nothing
                End If
            End If
            Dim pc As New DotNetNuke.Modules.Forum.PostController
            Dim pl As List(Of DotNetNuke.Modules.Forum.PostInfo) = pc.PostGetAllForThread(TopicId)
            Dim topicList As List(Of ContentInfo) = Nothing
            If Not pl Is Nothing AndAlso pl.Count > 0 Then
                topicList = New List(Of ContentInfo)
            End If
            For Each p As DotNetNuke.Modules.Forum.PostInfo In pl
                Dim ci As New ContentInfo
                With ci
                    .AuthorId = p.UserID
                    .AuthorName = p.Author.DisplayName
                    .Body = p.Body
                    .DateCreated = CStr(p.CreatedDate)
                    .ForumId = p.ForumID
                    If p.ParentPostID = -1 Then
                        .NextItem = p.ParentThread.NextThreadID
                        .PrevItem = p.ParentThread.PreviousThreadID
                    Else
                        .NextItem = -1
                        .PrevItem = -1
                    End If
                    .ReplyCount = p.ParentThread.Replies
                    .Subject = p.Subject
                    .TopicId = p.ThreadID
                End With
                topicList.Add(ci)
            Next
            Return topicList
        End Function
        Private Function CleanOutput(ByVal text As String) As String
            If String.IsNullOrEmpty(text) Then
                Return String.Empty
            End If
            text = HttpUtility.HtmlDecode(text)
            text = text.Replace("<br />", ControlChars.NewLine)
            text = text.Replace("<br>", ControlChars.NewLine)
            text = text.Replace("</p>", ControlChars.NewLine)
            text = text.Replace("<p>", String.Empty)
            Dim pattern As String = "<(.|\n)*?>"
            Return Regex.Replace(text, pattern, String.Empty).Trim
        End Function
        Friend Function GetUser(ByVal PortalId As Integer, ByVal UserId As Integer) As UserInfo
            Dim u As New UserInfo
            u.UserId = -1
            u.UserRoles = DotNetNuke.Common.glbRoleUnauthUser & ";" & DotNetNuke.Common.glbRoleAllUsers & ";"
            u.Username = String.Empty
            u.DisplayName = String.Empty
            If UserId > 0 Then
                Dim uc As New DotNetNuke.Entities.Users.UserController
                Dim objUser As DotNetNuke.Entities.Users.UserInfo = uc.GetUser(PortalId, UserId)
                If Not objUser Is Nothing Then
                    u.UserId = objUser.UserID
                    u.Username = objUser.Username
                    u.DisplayName = objUser.DisplayName
                    u.UserRoles = GetRoleIds(objUser.Roles, PortalId)

                End If
            End If
            Return u
        End Function
        Private Function GetRoleIds(ByVal Roles() As String, ByVal SiteId As Integer) As String
            Dim RoleIds As String = ""
            Dim rc As New DotNetNuke.Security.Roles.RoleController
            For Each r As DotNetNuke.Security.Roles.RoleInfo In rc.GetPortalRoles(SiteId)
                Dim roleName As String = r.RoleName
                For Each role As String In Roles
                    If role <> "" AndAlso Not role Is Nothing Then
                        If roleName = role Then
                            RoleIds &= r.RoleID.ToString & ";"
                            Exit For
                        End If
                    End If
                Next
            Next
            Return RoleIds
        End Function
    End Class
End Namespace

